package twitter.tool;

import java.io.IOException;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import twitter.entity.SimilarAccount;
import twitter.entity.Stat;
import twitter.entity.Tweet;
import twitter4j.Status;

public class Parser {

	public List<Tweet> parseTweets(List<Status> statuses) throws IOException {

		List<Tweet> tweets = new ArrayList<Tweet>();
		for (Status status : statuses) {

			Tweet tweet = new Tweet();
			tweet.setId(Long.toString(status.getId()));
			tweet.setDate(new Date(status.getCreatedAt().getTime()));
			tweet.setText(status.getText());
			tweet.setRetweet_count((int) status.getRetweetCount());
			tweet.setHashtags(status.getHashtagEntities());
			tweet.setUser_mentions(status.getUserMentionEntities());
			tweet.setFavorite_count((int) status.getFavoriteCount());
			if (status.getURLEntities().length > 0) {
				tweet.setURL(status.getURLEntities()[0].getExpandedURL());
				tweet.setContent(getContent(tweet.getURL()));
			} else {
				tweet.setContent(null);
			}
			if (status.getMediaEntities().length > 0) {
				if (status.getMediaEntities()[0].getType().equals("photo")) {
					tweet.setContent("photo");
				}

			}
			if (!tweets.contains(tweet)) {

				tweets.add(tweet);
			}

		}
		// tweet.setUrls(status.getURLEntities());
		return tweets;
	}

	private String getContent(String u) throws IOException {
		if (u.contains("play.google")) {
			return "app";
		}
		if (u.contains("youtube") || u.contains("vimeo")) {
			return "video";
		}
		if (u.contains("facebook") || u.contains("fb")) {
			return "facebook";
		}
		if (u.contains("spotify")) {
			return "spotify";
		}
		return "altro";
	}

	public List<Tweet> parseTweets(ResultSet rs) throws SQLException {
		List<Tweet> list = new ArrayList<Tweet>();

		while (rs.next()) {
			Tweet tweet = new Tweet();
			tweet.setId(rs.getString("id"));
			tweet.setDate(rs.getDate("date"));
			tweet.setText(rs.getString("text"));
			tweet.setRetweet_count(rs.getInt("retweets"));
			tweet.setFavorite_count(rs.getInt("favorites"));
			tweet.setSentiment(rs.getString("sentiment"));
			tweet.setContent(rs.getString("content"));
			String hashtags = rs.getString("hashtags");
			List<String> h = new ArrayList<String>();
			int i = 0;
			int temp = 0;
			while (i != -1) {
				i = hashtags.indexOf(",", i + 1);
				if (i != -1) {
					h.add(hashtags.substring(temp, i));
					temp = i + 1;
				}
			}
			h.add(hashtags.substring(temp));
			tweet.setHashtags(h);
			list.add(tweet);
		}

		return list;

	}

	public List<Stat> parseStats(ResultSet rs) throws SQLException {
		List<Stat> stats = new ArrayList<Stat>();
		while (rs.next()) {
			Stat s = new Stat();
			s.setDate(rs.getDate("date"));
			s.setFollower(rs.getInt("follower"));
			s.setFollowing(rs.getInt("following"));
			s.setAf(rs.getFloat("af"));
			s.setAr(rs.getFloat("ar"));
			s.setNt(rs.getInt("nt"));
			stats.add(s);
		}
		return stats;
	}

	public List<SimilarAccount> parseSimlarAccount(ResultSet rs)
			throws SQLException {
		List<SimilarAccount> similarAccounts = new ArrayList<SimilarAccount>();
		while (rs.next()) {
			SimilarAccount similarAccount = new SimilarAccount(
					rs.getString("id"), rs.getString("twittername"),
					rs.getString("facebookname"));
			similarAccounts.add(similarAccount);
		}
		return similarAccounts;
	}

	public List<SimilarAccount> parseSimilarAccount(String[] twitterNames,
			String[] facebookNames) {

		List<SimilarAccount> result = new ArrayList<SimilarAccount>();
		if (twitterNames.length == facebookNames.length) {
			for (int i = 0; i < twitterNames.length; i++) {
				result.add(new SimilarAccount(null, twitterNames[i],
						facebookNames[i]));
			}
		} else {
			System.out.println("Errore: Lunghezza dei due array diversa.");
		}
		return result;

	}

	// public Tweet parseTweet(JSONObject obj) throws JSONException{
	// Tweet tweet = new Tweet();
	// tweet.setId(obj.getString("id_str"));
	// tweet.setText(obj.getString("text"));
	// tweet.setRetweet_count(obj.getInt("retweet_count"));
	// tweet.setRetweeted(obj.getBoolean("retweeted"));
	// tweet.setFavorited(obj.getBoolean("favorited"));
	// JSONObject user = obj.getJSONObject("user");
	// tweet.setUser(parseUser(user));
	// JSONArray a = obj.getJSONArray("hashtags");
	// tweet.setHastags(parseArray(a));
	// a = obj.getJSONArray("user_mentions");
	// tweet.setUser_mentions(parseArray(a));
	// a = obj.getJSONArray("urls");
	// tweet.setUrls(parseUrls(a));
	//
	// return tweet;
	// }
	//
	// public ArrayList<Url> parseUrls(JSONArray a) throws JSONException {
	//
	// ArrayList<Url> urls = new ArrayList<Url>();
	//
	// for (int i = 0; i<a.length(); i++){
	// Url url = new Url();
	// url.setUrl(a.getJSONObject(i).getString("url"));
	// url.setExpanded_url(a.getJSONObject(i).getString("expanded_url"));
	// urls.set(i, url);
	// }
	//
	// return urls;
	// }
	//
	// private ArrayList<String> parseArray(JSONArray a) throws JSONException {
	//
	// ArrayList<String> array = new ArrayList<String>();
	//
	// for (int i = 0; i<a.length(); i++){
	// array.set(i, a.getString(i));
	// }
	//
	//
	// return array;
	// }
	//
	// public User parseUser(JSONObject obj) throws JSONException {
	// User user = new User();
	// user.setId(obj.getString("id_str"));
	// user.setName(obj.getString("name"));
	// user.setLocation(obj.getString("location"));
	// user.setDescription(obj.getString("description"));
	// user.setFollowers_count(obj.getInt("followers_count"));
	// user.setFriends_count(obj.getInt("friends_count"));
	// user.setStatuses_count(obj.getInt("statusescount"));
	// user.setTime_zone(obj.getString("time_zone"));
	//
	// return user;
	// }
	//
}
